<?php

/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */

/**
 * Wraps the local cache driver.
 *
 * PHP version 5
 *
 * Copyright 2008 The Picnic PHP Framework
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.

 * @category      Framework
 * @package       Pfw
 * @author        Sean Sitter <sean@picnicphp.com>
 * @copyright     2008 The Picnic PHP Framework
 * @license       http://www.apache.org/licenses/LICENSE-2.0
 * @link          http://www.picnicphp.com
 * @since         0.10
 */

class Pfw_Cache_Local {
    private static $instance = null;

    /**
     * Resets the instance of the cache driver
     */
    public static function _reset()
    {
        self::$instance = null;
    }

    /**
     * Returns an instance of the local cache driver
     * 
     * @return Pfw_Cache|null instance of a Pfw_Cache object or null if none
     * is configured
     */
    public static function getInstance()
    {
        if (!is_null(self::$instance)) {
            return self::$instance;
        }
        
        if (null == ($local_cache = Pfw_Config::get('local_cache'))) {
            return null;
        }
        
        if (is_array($local_cache)) {
            $class = $local_cache['class'];
            unset($local_cache['class']);
            $options = $local_cache;
        } else {
            $class = $local_cache;
            $options = array();
        }
        Pfw_Loader::loadClass($class);
        self::$instance = new $class($options);
        
        return self::$instance;
    }

    /**
     * @see Pfw_Cache#get()
     */
    public static function get($key)
    {
        if (!self::isActive()) {
            return false;
        }
        return self::getInstance()->get($key);
    }

    /**
     * @see Pfw_Cache#set()
     */
    public static function set($key, $value, $ttl_s = null)
    {
        if (!self::isActive()) {
            return false;
        }
        return self::getInstance()->set($key, $value, $ttl_s);
    }
    
    /**
     * @see Pfw_Cache#add()
     */
    public static function add($key, $value, $ttl_s = null)
    {
        if (!self::isActive()) {
            return false;
        }
        return self::getInstance()->add($key, $value, $ttl_s);
    }

    /**
     * @see Pfw_Cache#delete()
     */
    public static function delete($key)
    {
        if (!self::isActive()) {
            return false;
        }
        return self::getInstance()->delete($key);
    }

    /**
     * @see Pfw_Cache#isActive()
     */
    public static function isActive()
    {
        if (null === self::getInstance()) {
            return false;
        }
        return true;
    }

    /**
     * Is our instance of the local cache driver active?
     * 
     * @return bool
     */
    public function isInstanceActive()
    {
        return self::$instance->isActive();
    }
}
